# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
import netCDF4
from netCDF4 import Dataset
nc_obj=Dataset('./data/cru_ts4.02.1901.2017.pre.dat.nc')



#查看nc文件有些啥东东
print(nc_obj)
print('---------------------------------------')

#查看nc文件中的变量
print(nc_obj.variables.keys())
for i in nc_obj.variables.keys():
    print(i)
print('---------------------------------------')
#>>
# odict_keys(['lon', 'lat', 'time', 'pre', 'stn'])
# lon
# lat
# time
# pre
# stn

#查看每个变量的信息
print("variables info:")
print(nc_obj.variables['lat'])  # latitude, [float32] units: degrees_north
print(nc_obj.variables['lon'])  # longitude,  [float32] units: degrees_east
print(nc_obj.variables['time']) # time, [float32] units: days since 1900-1-1, calendar: gregorian
print(nc_obj.variables['pre'])  # precipitation, [float32] units: mm/month,   //降水 每个单元包含 time lat lon ;pre(time, lat, lon)
print(nc_obj.variables['stn'])  # stn, [int32] description: number of stations contributing to each datum 【站点号】 -999 应该是无效数据吧？？
print('---------------------------------------')

#>>
# variables info:

# <class 'netCDF4._netCDF4.Variable'>
# float32 lat(lat)
#     long_name: latitude
#     units: degrees_north
# unlimited dimensions:
# current shape = (360,)
# filling on, default _FillValue of 9.969209968386869e+36 used

# <class 'netCDF4._netCDF4.Variable'>
# float32 lon(lon)
#     long_name: longitude
#     units: degrees_east
# unlimited dimensions:
# current shape = (720,)
# filling on, default _FillValue of 9.969209968386869e+36 used

# <class 'netCDF4._netCDF4.Variable'>
# float32 time(time)
#     long_name: time
#     units: days since 1900-1-1
#     calendar: gregorian
# unlimited dimensions: time
# current shape = (1404,)
# filling on, default _FillValue of 9.969209968386869e+36

# <class 'netCDF4._netCDF4.Variable'>
# float32 pre(time, lat, lon)
#     long_name: precipitation
#     units: mm/month
#     correlation_decay_distance: 450.0
#     _FillValue: 9.96921e+36
#     missing_value: 9.96921e+36
# unlimited dimensions: time
# current shape = (1404, 360, 720)
# filling on

# <class 'netCDF4._netCDF4.Variable'>
# int32 stn(time, lat, lon)
#     description: number of stations contributing to each datum
# unlimited dimensions: time
# current shape = (1404, 360, 720)
# filling on, default _FillValue of -2147483647 used

#
# #查看每个变量的属性
print(nc_obj.variables['lon'].ncattrs()) # ['long_name', 'units']
print(nc_obj.variables['lat'].ncattrs()) # ['long_name', 'units']
print(nc_obj.variables['time'].ncattrs()) # ['long_name', 'units', 'calendar']
print(nc_obj.variables['pre'].ncattrs()) # ['long_name', 'units', 'correlation_decay_distance', '_FillValue', 'missing_value']
print(nc_obj.variables['stn'].ncattrs()) # ['description']
print('---------------------------------------')

# #读取数据值
lon=(nc_obj.variables['lon'][:])
lat=(nc_obj.variables['lat'][:])
time=(nc_obj.variables['time'][:])
pre=(nc_obj.variables['pre'][:])
stn=(nc_obj.variables['stn'][:])

# print(lon)
# print(lat)
# print(time)
# print(pre)
# print(stn)
